package com.guessdroid.sensing;

import java.util.ArrayList;


public class AS_avgAudioVolume extends AbstractSensor implements java.io.Serializable
{
	private static final long serialVersionUID = -6320395505771998749L;
	public final String name="avg Audio volume";

	// Assumes 44100 Hz sample rate
	// Assumes big-endian short content
	public ArrayList<Float> process(RawData rawdata, int interval)
	{
		ArrayList<Float> ret=new ArrayList<Float>();
		
		int i=0;long accumulator=0;double interval_count=0;
		while (i<rawdata.auData.size())
		{
			int ii=0;
			while (ii<rawdata.auData.get(i).length)
			{
				accumulator+=rawdata.auData.get(i)[ii];
				interval_count+=((double)interval)/44100;
				if (interval_count>interval)
				{
					// average w.r.t. a global interval. divide by c*interval if differing intervals
					ret.add((float)accumulator);
					interval_count-=interval;
					accumulator=0;
				}
				ii++;
			}
			i++;
		}
		
		return ret;
	}

}
